/*
 * Copyright (c) 2008-2009, David Armitage
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. The names of its contributors may not be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package jfxmines;

import javafx.io.*;
import java.io.InputStream;
import java.io.OutputStream;
import javafx.util.Properties;

/**
 * @author David Armitage
 */
var p: Properties = Properties{};

public function getStore() {
  var storage: Storage;
  var resource: Resource;
  var is: InputStream;
  var pString: String;

  storage = Storage { source: "JFXMinesStore" };
  resource = storage.resource;
  is = resource.openInputStream();
  p.load(is);
  Main.mfSet = getNumProp("mfSet", 1) as Integer;
  Main.stageX = getNumProp("stageX", 300);
  Main.stageY = getNumProp("stageY", 300);
  Main.fastestEasy = getNumProp("fastestEasy", 999) as Integer;
  Main.fastestIntermediate = getNumProp("fastestIntermediate", 999) as Integer;
  Main.fastestHard = getNumProp("fastestHard", 999) as Integer;
  if (p.get("isSoundOn") == null) then Main.isSoundOn = true else Main.isSoundOn = (p.get("isSoundOn") == "true");
  is.close();
  }

function getNumProp(pName: String, pDefault: Float): Float {
  var pString: String;

  pString = p.get(pName);
  if (pString == null)
    {
    return pDefault;
    }
   else
    {
    return Number.parseFloat(pString);
    }
  }

public function setStore(reset: Boolean) {
  var storage: Storage;
  var resource: Resource;
  var os: OutputStream;

  storage = Storage { source: "JFXMinesStore" };
  resource = storage.resource;
  os = resource.openOutputStream(true);
  p.put("mfSet","{Main.mfSet}");
  p.put("stageX","{Main.stageX}");
  p.put("stageY","{Main.stageY}");
  if (reset)
    {
    p.put("fastestEasy","999");
    p.put("fastestIntermediate","999");
    p.put("fastestHard","999");
    }
   else 
    {
    p.put("fastestEasy","{Main.fastestEasy}");
    p.put("fastestIntermediate","{Main.fastestIntermediate}");
    p.put("fastestHard","{Main.fastestHard}");
    }
  p.put("isSoundOn", "{Main.isSoundOn}");
  p.store(os);
  os.close();
  }



